package at.fhooe.mc.pro.treeproject;
/**
 * @author Thomas Kranzer, Mario Baumgartner
 * 
 */
public class TreeNode {
	int value;
	TreeNode left;
	TreeNode right;

	TreeNode() {

	}

	TreeNode(int val) {
		value = val;
	}

	/**
	 * searchNode returns true if the node with the value x is in the TreeNode
	 * and false if not.
	 * 
	 * @param tree
	 *            as TreeNode
	 * @param x
	 *            as int
	 * @return true or false
	 */
	public boolean searchNode(TreeNode tree, int x) {

		if (tree == null) {
			return false;
		}
		if (tree.value == x) {
			return true;
		}

		if (x < tree.value) {
			return searchNode(tree.left, x);
		} else {
			return searchNode(tree.right, x);
		}
	}

	/**
	 * prints the tree pre order
	 * 
	 * @param tree
	 *            as TreeNode
	 */
	public void printTreePreOrder(TreeNode tree) {
		if (tree != null) {
			System.out.print(tree.value + ",");
			printTreePreOrder(tree.left);
			printTreePreOrder(tree.right);
		}
	}

	/**
	 * prints the tree in order
	 * 
	 * @param tree
	 *            as TreeNode
	 */
	public void printTreeInOrder(TreeNode tree) {
		if (tree != null) {
			printTreeInOrder(tree.left);
			System.out.print(tree.value + ",");
			printTreeInOrder(tree.right);
		}
	}

	/**
	 * prints the tree post order
	 * 
	 * @param tree
	 *            as TreeNode
	 */
	public void printTreePostOrder(TreeNode tree) {
		if (tree != null) {
			printTreePostOrder(tree.left);
			printTreePostOrder(tree.right);
			System.out.print(tree.value + ",");
		}
	}

	/**
	 * prints the tree in an other undefined order
	 * 
	 * @param tree
	 *            as TreeNode
	 */
	public void printTreeAndersOrder(TreeNode tree) {
		if (tree != null) {
			printTreeAndersOrder(tree.right);
			System.out.print(tree.value + ",");
			printTreeAndersOrder(tree.left);
		}
	}
}
